//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLDatePresentViewController.h"
@interface BLDatePresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) UIDatePicker                     *datePicker;/**< <##> */
@end

@implementation BLDatePresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 100)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    self.customView.centerY = UIScreenHeight / 2;
    [self.view addSubview:self.customView];
    
    
    UIView * view = [TMUtils headerViewWithTitle:@"请选择入驻时间"];
    [self.customView addSubview:view];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:@"确认"
                                                titleColor:BL_BlueBackGroundColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    confimButton.right = UIScreenWidth - 15;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [confimButton setClicAction:^(UIButton *sender) {
        UIDatePicker *picker = weakSelf.datePicker;
        NSDate *date = [picker date];
        NSLog(@"选择的时间%@",date);

        [weakSelf dismissWithHandle:^{
            if (weakSelf.didChosedTime) {
                weakSelf.didChosedTime(@[date]);
            }
        }];;
    }];
    confimButton.centerY = view.centerY;
    [self.customView addSubview:confimButton];
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.frame = CGRectMake(0, view.bottom + 10, UIScreenWidth, 260);
    NSDateFormatter * formate = [[NSDateFormatter alloc]init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm"];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    
    _datePicker.minimumDate = [NSDate date];
    _datePicker.maximumDate = [NSDate dateWithTimeIntervalSince1970:NSDate.st_localCurrentDate.timeIntervalSince1970 + 60 * 60 * 24 * 365];
    [self.customView addSubview:_datePicker];
    self.customView.height = _datePicker.bottom;
    //        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [_datePicker addTarget:self action:@selector(datepickerAction:) forControlEvents:UIControlEventTouchDown];
    self.customView.top = UIScreenHeight ;
}

#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.bottom = UIScreenHeight;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
#pragma mark --Action Method
-(void)datepickerAction:(UIDatePicker*)sender
{
    
    //将时间转换成本地时间

}
@end







